'use strict';

var _ = require('lodash');
var Department = require('./department.model');

// Get list of departments
exports.index = function(req, res) {
  Department.find(function (err, departments) {
    if(err) { return handleError(res, err); }
    return res.json(200, departments);
  });
};

// Get a single department
exports.show = function(req, res) {
  Department.findById(req.params.id, function (err, department) {
    if(err) { return handleError(res, err); }
    if(!department) { return res.send(404); }
    return res.json(department);
  });
};

// Creates a new department in the DB.
exports.create = function(req, res) {
  Department.create(req.body, function(err, department) {
    if(err) { return handleError(res, err); }
    return res.json(201, department);
  });
};

// Updates an existing department in the DB.
exports.update = function(req, res) {
  if(req.body._id) { delete req.body._id; }
  Department.findById(req.params.id, function (err, department) {
    if (err) { return handleError(res, err); }
    if(!department) { return res.send(404); }
    var updated = _.merge(department, req.body);
    updated.save(function (err) {
      if (err) { return handleError(res, err); }
      return res.json(200, department);
    });
  });
};

// Deletes a department from the DB.
exports.destroy = function(req, res) {
  Department.findById(req.params.id, function (err, department) {
    if(err) { return handleError(res, err); }
    if(!department) { return res.send(404); }
    department.remove(function(err) {
      if(err) { return handleError(res, err); }
      return res.send(204);
    });
  });
};

exports.first = function (req, res) {
  var User = require('../user/user.model');

  Department.find({}).remove(function() {
    Department.create({
      name : '综合部'
    },{
      name : '财务部'
    },{
      name : '工程部'
    },{
      name : '技术部'
    },{
      name : '业务部'
    },{
      name : '材料部'
    },{
      name : '售后部'
    });
  });

  User.find({}).remove(function() {
    User.create({
        provider: 'local',
        role: '总经理',
        name: 'Admin',
        phone: '18758323417',
        email: 'admin@admin.com',
        password: 'admin'
      }, function() {
        return res.send('finished populating users');
      }
    );
  });

};

function handleError(res, err) {
  return res.send(500, err);
}
